/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aviacad;

import aviacad.exceptions.NonexistentEntityException;
import aviacad.exceptions.PreexistingEntityException;
import com.sun.xml.internal.ws.util.StringUtils;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.persistence.*;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.Metamodel;
/**
 *
 * @author Юминова Анна
 */
public class ScheduleJpaController implements Serializable {

    public ScheduleJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Schedule schedule) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(schedule);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findSchedule(schedule.getFlightNumber()) != null) {
                throw new PreexistingEntityException("Schedule " + schedule + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Schedule schedule) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            schedule = em.merge(schedule);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = schedule.getFlightNumber();
                if (findSchedule(id) == null) {
                    throw new NonexistentEntityException("The schedule with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Schedule schedule;
            try {
                schedule = em.getReference(Schedule.class, id);
                schedule.getFlightNumber();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The schedule with id " + id + " no longer exists.", enfe);
            }
            em.remove(schedule);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Schedule> findScheduleEntities() {
        return findScheduleEntities(true, -1, -1);
    }

    public List<Schedule> findScheduleEntities(int maxResults, int firstResult) {
        return findScheduleEntities(false, maxResults, firstResult);
    }

    private List<Schedule> findScheduleEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Schedule.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Schedule findSchedule(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Schedule.class, id);
        } finally {
            em.close();
        }
    }

    public int getScheduleCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Schedule> rt = cq.from(Schedule.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
     public Schedule getFlight(String city_from, String city_to) {
       EntityManager em = getEntityManager();
       try {
           Query query = em.createNamedQuery("Schedule.findByCityToAndFrom");
           query.setParameter("cityTo", city_to);
           query.setParameter("cityFrom", city_from);
           return (Schedule) query.getSingleResult();
           
       }finally {
           em.close();
       }
           }
    public List<Schedule> getSchedule(String cityFrom, String cityTo ) {
    String queryString = "SELECT s FROM Schedule s " +
                         "WHERE s.cityFrom = :cityFrom AND s.cityTo = :cityTo";
    Query query = getEntityManager().createQuery(queryString);
    query.setParameter("cityFrom", cityFrom);
    query.setParameter("cityTo", cityTo);
    return query.getResultList();
}
     public static void main(String[] args) {
          
     EntityManagerFactory dbEntityFactory = Persistence.createEntityManagerFactory("AviacadPU");
     ScheduleJpaController s = new ScheduleJpaController(dbEntityFactory);
     List <Schedule> schedule = s.getSchedule("SVO-D", "LED");
     for(Schedule shed : schedule) System.out.println(shed.getFlightNumber());
    
}}
